#ifndef DATABUFFER_H
#define DATABUFFER_H

#include <QByteArray>
#include <QMap>
#include <QMutex>
#include <QThread>


class DataBuffer : public QThread
{
public:
    ~DataBuffer();

    static DataBuffer &instance();
    struct DataNode
    {
        DataNode( const QString &key = QString(), const QString &value = QString(), bool vaild = true )
        {
            this->vaild    = vaild;
            this->key      = key;
            this->key_by   = stringToByteArray( key );
            this->value    = value;
            this->value_by = stringToByteArray( value );
        }
        bool       vaild;
        QString    key;
        QByteArray key_by;
        QString    value;
        QByteArray value_by;
    };
    QMap<int, DataBuffer::DataNode> getDataBuffer();

    void setDataVaild( const int &id, const bool vaild );
    void setDataKey( const int &id, const QString &key );
    void setDataValue( const int &id, const QString &value );

    bool matchDataValue( const QByteArray &key, QByteArray &value );

protected:
    virtual void run() override;

private:
    DataBuffer();

private:
    static QByteArray stringToByteArray( const QString &str );

private:
    void load();
    void save();

private:
    QRecursiveMutex     _mutex;
    QMap<int, DataNode> _all_buffer_map;
};

#endif  // DATABUFFER_H
